﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class JoyStick : ScrollRect
{

    public float Radius { get; private set; }

    public Vector2 Axis { get; private set; }

    protected override void Start()
    {
        base.Start();
        Axis = Vector2.zero;
        Radius = (transform as RectTransform).sizeDelta.x * 0.5f;
    }
    public override void OnDrag(PointerEventData eventData)
    {
        base.OnDrag(eventData);
        Vector2 position = content.anchoredPosition;
        if(position.magnitude > Radius)
        {
            position = position.normalized * Radius;
            SetContentAnchoredPosition(position);
        }
        Axis = content.anchoredPosition.normalized;
    }
    public override void OnEndDrag(PointerEventData eventData)
    {
        base.OnEndDrag(eventData);
        Axis = Vector2.zero;
    }
}
